package com.jhws.business.appconfig.controller;

import com.jhws.common.base.bo.JsonRespData;
import com.jhws.business.appconfig.bo.AppConfigType;
import com.jhws.business.appconfig.service.AppConfigTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 配置类型控制器
 */
@Controller
@RequestMapping("/appConfig/type")
public class AppConfigTypeController {

    @Resource
    private AppConfigTypeService appConfigTypeService;

    /**
     * 保存类型
     *
     * @param appConfigType 类型信息
     */
    @RequestMapping("saveAppConfigType.x")
    @ResponseBody
    public JsonRespData saveAppConfigType(AppConfigType appConfigType) throws Exception {
        appConfigTypeService.save(appConfigType);
        return new JsonRespData("", 1, "添加类型成功");
    }

    /**
     * 查询类型
     *
     * @return 类型
     */
    @RequestMapping("queryAppConfigTypes.x")
    @ResponseBody
    public JsonRespData queryAppConfigTypes(String deviceType) {
        List<AppConfigType> appConfigTypes = appConfigTypeService.queryAppConfigTypeByWhere("where parent_id='-1' and device_type='" + deviceType + "'");
        return new JsonRespData("", 1, appConfigTypes);
    }

    /**
     * 查询所有类型父子关系
     *
     * @return 类型
     */
    @RequestMapping("queryAppConfigTypeAll.x")
    @ResponseBody
    public JsonRespData queryAppConfigTypeAll(String deviceType) {
        List<AppConfigType> appConfigTypes = appConfigTypeService.queryAppConfigTypeByWhere("where device_type='" + deviceType + "' or device_type='all'");
        List<AppConfigType> nodeList = new ArrayList<>();
        for (AppConfigType node1 : appConfigTypes) {
            boolean mark = false;
            for (AppConfigType node2 : appConfigTypes) {
                if (node1.getParentId().equals(node2.getId())) {
                    mark = true;
                    if (node2.getChildren() == null) {
                        node2.setChildren(new ArrayList<>());
                    }
                    node2.getChildren().add(node1);
                    break;
                }
            }
            if (!mark) {
                nodeList.add(node1);
            }
        }
        return new JsonRespData("", 1, nodeList);
    }

    /**
     * 分页查询类型信息
     *
     * @param param Map
     * @return 类型信息列表
     */
    @RequestMapping("queryAppConfigTypePage.x")
    @ResponseBody
    public JsonRespData queryAppConfigTypePage(@RequestParam Map param) {
        Map data = appConfigTypeService.queryAppConfigTypePage(param);
        return new JsonRespData("", 1, data);
    }

    /**
     * 删除类型信息
     *
     * @param appConfigType 类型信息ID
     */
    @RequestMapping("deleteAppConfigType.x")
    @ResponseBody
    public JsonRespData deleteAppConfigType(AppConfigType appConfigType, boolean isType) throws Exception {
        if (isType) {
            appConfigTypeService.deleteAppConfigType("where id='" + appConfigType.getId() + "' or parent_id='" + appConfigType.getId() + "'");
        } else {
            appConfigTypeService.deleteAppConfigType(appConfigType);
        }
        return new JsonRespData("", 1, "删除成功");
    }
}
